package com.wcgksoft.controller.plant;


import com.wcgksoft.common.util.Layui;
import com.wcgksoft.common.util.MsgInfo;
import com.wcgksoft.entity.GreedLand.GreedLand;
import com.wcgksoft.entity.Plant.Plant;
import com.wcgksoft.entity.Plant.PlantEncyclopedias;
import com.wcgksoft.entity.Plant.PlantType;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.GreedLand.GreedlandService;
import com.wcgksoft.service.plant.PlantEncyclopediasService;
import com.wcgksoft.service.plant.PlantService;
import com.wcgksoft.service.plant.PlantTypeService;
import com.wcgksoft.service.system.UserService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 植物管理
 *
 * @author Administrator
 */
@Controller
@RequestMapping("plant")
public class PlantController {

    @Autowired
    private PlantService plantService;
    @Autowired
    private PlantTypeService plantTypeService;
    @Autowired
    private PlantEncyclopediasService plantEncyclopediasService;
    @Autowired
    private UserService UserService;

    @Autowired
    private GreedlandService greedlandService;
    /**
     * 列表单个绿地
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list")
    public Layui list(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "limit", required = false) Integer limit,
            @RequestParam(value = "greedLandid", required = false) Integer greedLandid
            ) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Plant.class);
        criteria.createAlias("greedLand", "greedLand");
        criteria.add(Restrictions.eq("greedLand.id",greedLandid));
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        criteria.add(Restrictions.eq("departid", sysUser.getProId()+""));
        criteria.add(Restrictions.eq("areaid", enterpriseProject.getId()+""));
        List<Plant> list = plantService.findByConditions(criteria, page, limit);
        if(list.size()>0){
            for (int i = 0;i<list.size();i++){
                //项目
                String sysuserid = list.get(i).getUserid();
                SysUser User = UserService.findById(Integer.valueOf(sysuserid));
                list.get(i).setSysUser(User);

            }

        }
        return Layui.data(0, "查询成功", plantService.getTotalCount(), list);
    }

    /*
     * 修改
     *
     * */
    @ResponseBody
    @RequestMapping(value = "update")
    public Layui update(HttpServletRequest request, @RequestParam(value = "data", required = false) String data) throws Exception {
        JSONObject param = JSONObject.fromObject(data);
        if (param == null) {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            Plant Plant = (Plant) JSONObject.toBean(JSONObject.fromObject(data), Plant.class);
            String ids = String.valueOf(param.get("id"));
            if (ids.equals("")) {
                Plant.setUp_time(new Date());
            }
            SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
            EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
            Plant.setDepartid(sysUser.getProId()+"");
            Plant.setAreaid(enterpriseProject.getId()+"");
            Plant.setEnterpriseid(sysUser.getEnterprise().getId());
            
            String greedLand = String.valueOf(param.get("greedLand_id"));
            Integer greedLand_id=Integer.valueOf(greedLand);
            GreedLand byId = greedlandService.findById(greedLand_id);
            Plant.setGreedLand(byId);

            String plantType = String.valueOf(param.get("plantType_id"));
            Integer plantType_id=Integer.valueOf(plantType);
            PlantType byId1 = plantTypeService.findById(plantType_id);
            Plant.setPlantType(byId1);

            String plantEncyclopedias = String.valueOf(param.get("plantEncyclopedias_id"));
            Integer plantEncyclopedias_id=Integer.valueOf(plantEncyclopedias);
            PlantEncyclopedias byId2 = plantEncyclopediasService.findById(plantEncyclopedias_id);
            Plant.setPlantEncyclopedias(byId2);

            Plant.setUserid(sysUser.getId()+"");

            plantService.saveOrUpdate(Plant);
            return Layui.data(0, "修改成功", 1, Layui.addList(Plant));
        }
    }

    /*
     * 删除
     *
     * */
    @ResponseBody
    @RequestMapping(value = "delete")
    public Layui delete(@RequestParam(value = "id") Integer id) {
        Plant byId = plantService.findById(id);
        plantService.delete(byId);
        return Layui.data(1, "删除成功", 1, null);
    }

    /*
     * 查看植物百科
     *
     * */
    @ResponseBody
    @RequestMapping(value = "details")
    public MsgInfo details(@RequestParam(value = "id") Integer id) {
        MsgInfo msgInfo = new MsgInfo();
        PlantEncyclopedias byId = plantEncyclopediasService.findById(id);
        msgInfo.setData(byId);
        return msgInfo;
    }
    /*
     * 单个绿地根据植物类型统计树木数量面积
     *
     * */
    @ResponseBody
    @RequestMapping(value = "typetrees")
    public MsgInfo typetrees (
            @RequestParam(value = "greedLandid", required = false) Integer greedLandid) {
        MsgInfo msgInfo = new MsgInfo();
        DetachedCriteria criteria = DetachedCriteria.forClass(Plant.class);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        criteria.add(Restrictions.eq("departid", sysUser.getProId()+""));
        criteria.add(Restrictions.eq("areaid", enterpriseProject.getId()+""));
        criteria.createAlias("greedLand", "greedLand");
        criteria.add(Restrictions.eq("greedLand.id",greedLandid));
        List<Plant> list = plantService.findByConditions(criteria);

        DetachedCriteria criteriat = DetachedCriteria.forClass(PlantType.class);
        criteriat.add(Restrictions.eq("enterpriseid", sysUser.getEnterprise().getId()));
        criteriat.add(Restrictions.eq("state",0));
        List<PlantType> listt = plantTypeService.findByConditions(criteriat);
        if(list.size()>0){
            int tnum;
            Double mian;
           for(int i=0;i<listt.size();i++){
               tnum=0;mian=0.0;
               for(int j=0;j<list.size();j++){
                    if(list.get(j).getPlantType().getId().equals(listt.get(i).getId())){
                        tnum+=list.get(j).getQuantity();
                        listt.get(i).setState(tnum);
                        mian+=list.get(j).getArea();
                        listt.get(i).setArea(mian);
                    }
               }
           }
        }
        msgInfo.setData(listt);
        return msgInfo;
    }


    /*
     * 植物管理 植物统计树木数量面积
     *
     * */
    @ResponseBody
    @RequestMapping(value = "typetree")
    public MsgInfo typetree () {
        MsgInfo msgInfo = new MsgInfo();
        DetachedCriteria criteria = DetachedCriteria.forClass(Plant.class);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        criteria.add(Restrictions.eq("departid", sysUser.getProId()+""));
        criteria.add(Restrictions.eq("areaid", enterpriseProject.getId()+""));
        List<Plant> list = plantService.findByConditions(criteria);

        DetachedCriteria criteriat = DetachedCriteria.forClass(PlantType.class);
        criteriat.add(Restrictions.eq("enterpriseid", sysUser.getEnterprise().getId()));
        criteriat.add(Restrictions.eq("state",0));
        List<PlantType> listt = plantTypeService.findByConditions(criteriat);
        if(list.size()>0){
            int tnum;
            Double mian;
            for(int i=0;i<listt.size();i++){
                tnum=0;mian=0.0;
                for(int j=0;j<list.size();j++){
                    if(list.get(j).getPlantType().getId().equals(listt.get(i).getId())){
                        tnum+=list.get(j).getQuantity();
                        listt.get(i).setState(tnum);
                        mian+=list.get(j).getArea();
                        listt.get(i).setArea(mian);
                    }
                }
            }
        }
        msgInfo.setData(listt);
        return msgInfo;
    }

    /**
     * 植物管理列表（查询百科表）
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "lists")
    public Layui lists(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "limit", required = false) Integer limit,
            @RequestParam(value = "keyWord", required = false) String keyWord
    ) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Plant.class);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        criteria.add(Restrictions.eq("departid", sysUser.getProId()+""));
        criteria.add(Restrictions.eq("areaid", enterpriseProject.getId()+""));
        List<Plant> list = plantService.findByConditions(criteria);

        DetachedCriteria criteriat = DetachedCriteria.forClass(PlantEncyclopedias.class);

        criteriat.add(Restrictions.eq("enterpriseid", sysUser.getEnterprise().getId()));
        if(StringUtils.isNotEmpty(keyWord)) {
            criteriat.add(Restrictions.like("name", keyWord, MatchMode.ANYWHERE));
        }
        List<PlantEncyclopedias> listt = plantEncyclopediasService.findByConditions(criteriat);
        List<PlantEncyclopedias> listc = new ArrayList<PlantEncyclopedias>();
        //累加
        if(list.size()>0){
           int tnum;
            for(int i=0;i<listt.size();i++){
                if(listt.size()>0){
                tnum=0;
                for(int j=0;j<list.size();j++){
                    if(list.get(j).getPlantEncyclopedias().getId().equals(listt.get(i).getId())){
                        tnum+=list.get(j).getQuantity();
                        listt.get(i).setNum(tnum);
                    }
                }
                }
            }
            //去0
            if(listt.size()>0) {
                for (int i = 0; i < listt.size(); i++) {
                    if (listt.get(i).getNum()!=null) {
                        listc.add(listt.get(i));
                    }
                }
            }
        }


        return Layui.data(0, "查询成功", plantService.getTotalCount(), listc);
    }


    /**
     * 单个树种统计
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "listtj")
    public Layui listtj(
            @RequestParam(value = "eid", required = false) Integer plantEncyclopediasid
    ) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Plant.class);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        criteria.add(Restrictions.eq("departid", sysUser.getProId()+""));
        criteria.add(Restrictions.eq("areaid", enterpriseProject.getId()+""));
        criteria.createAlias("plantEncyclopedias", "plantEncyclopedias");
        criteria.add(Restrictions.eq("plantEncyclopedias.id",plantEncyclopediasid));
        List<Plant> list = plantService.findByConditions(criteria);
        return Layui.data(0, "查询成功", plantService.getTotalCount(), list);
    }
}
